package phone.qyg.com.qyg.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.adapter.recycler.CommonAdapter;
import com.app.adapter.recycler.MultiItemTypeAdapter;
import com.app.adapter.recycler.base.ViewHolder;
import com.app.application.QXApplication;
import com.app.base.tool.BaseActivity;
import com.app.config.ApplicationConfig;
import com.app.utils.JSONTool;
import com.app.utils.TransformController;
import com.app.view.BaseViewStateLayout;
import com.app.xutils.MyCallBack;
import com.app.xutils.Xutils;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.List;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Base;

/**
 * 作者：lblbh on 2018/1/16 15:23
 * 邮箱：lanbuhan@163.com
 * 功能：文化超市分类
 */
@ContentView(R.layout.culture_type)
public class CultureTypeActivity extends BaseActivity {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.linear)
    LinearLayout linear;

    BaseViewStateLayout baseView;

    RecyclerView recycler;

    int row = 3;

    @Override
    public void initSucessView() {
        baseView = new BaseViewStateLayout(QXApplication.getContext()) {

            @Override
            public View obtionView() {
                View sucessView = View.inflate(QXApplication.getContext(), R.layout.base_spring_recycler, null);
                recycler = (RecyclerView) sucessView.findViewById(R.id.recycler);
                GridLayoutManager gridLayoutManager = new GridLayoutManager(QXApplication.getContext(),row,GridLayoutManager.VERTICAL,false);
//                LinearLayoutManager layoutVerManager = new LinearLayoutManager(QXApplication.getContext());
//                layoutVerManager.setOrientation(LinearLayoutManager.VERTICAL);
//                recycler.addItemDecoration(new RecyclerViewItemDecoration(
//                        RecyclerViewItemDecoration.MODE_GRID,
//                        getResources().getColor(R.color.divider_two),
//                        CommonUtil.dip2px(QXApplication.getContext(), 1), 0, 0));
                recycler.setLayoutManager(gridLayoutManager);
                return sucessView;
            }

            @Override
            public Object obtionData() {
                getInitNetwork();
                return DELFAUTSTATE;
            }
        };

        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        linear.addView(baseView, layoutParams);
        x.view().inject(linear);
    }


    /**
     * 执行网络请求
     */
    private void getInitNetwork() {


        Xutils.Get(ApplicationConfig.NET_DSN_PATH + ApplicationConfig.RES_SUB_JECT_SELECT_CONDITION,
                mapKeys,
                new MyCallBack<String>() {
                    @Override
                    public void onSuccess(String result) {
                        super.onSuccess(result);

                        List<Base> temps = JSONTool.jsonDefaluTranClazzs(result, JSONTool.Enum.RES_TYPE_LIST, Base.class);


                        if (!isRequestList(temps)) {
                            baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_EMPTY);
                            baseView.stateView();
                            return;
                        }
                        //成功获取到数据
                        baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_SUCESS);
                        baseView.stateView();

                        setModelData(temps);
                    }

                    @Override
                    public void onError(Throwable ex, boolean isOnCallback) {
                        super.onError(ex, isOnCallback);
                        baseView.setThreadOrState(BaseViewStateLayout.BaseViewState.STATE_ERROE);
                        baseView.stateView();
                    }

                    @Override
                    public void onFinished() {
                        super.onFinished();
                    }
                });
    }


    private void setModelData( List<Base> models) {

        final CommonAdapter adapter = new CommonAdapter<Base>(QXApplication.getContext(), R.layout.culture_item_type, models) {
            @Override
            protected void convert(ViewHolder holder, Base model, int position) {
                holder.setText(R.id.item_title, model.title);
                holder.setBackgroundRes(R.id.item_rela,(((position/row)&1) != 1)?R.color.divider:R.color.white);
            }
        };

        adapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                Base base = (Base) adapter.getData().get(position);
//                TransformController.transformControllerModel(QXApplication.getContext(),CultureListActivity.class,obj);
                Intent intent = new Intent(ApplicationConfig.CULTURE_QX_URL);
                intent.putExtra(TransformController.ChangeKEY.INTENT_KEY, base.url);
                LocalBroadcastManager.getInstance(QXApplication.getContext()).sendBroadcast(intent);
                CultureTypeActivity.this.finish();
            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });

        recycler.setAdapter(adapter);
    }

    @Event(value = {R.id.back})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {}

    @Override
    public void initViewHandler() {}
}
